package model.element;

import java.awt.Rectangle;

import model.Element;
import model.World;

public class ElementFactory {
	public Road createRoad(int x, int y, World world){
		Road r = new Road(x, y, Road.ROAD_SIZE, Road.ROAD_SIZE);
		
		/* add nearby roads */
		for(int col = -1; col <= 1; col++){
			for(int row = -1; row <= 1; row++){
				if((col == 0 || row == 0) && (col != row)){
					Road tempRoad = world.getRoad(x + col*Road.ROAD_SIZE, y + row*Road.ROAD_SIZE);
					if(tempRoad != null){
						r.getNearbyRoads().add(tempRoad);
						tempRoad.getNearbyRoads().add(r);
					}
				}
			}
		}
		return r;
	}

	public Element createRoad(Rectangle rect, World world) {
		return createRoad(rect.x, rect.y, world);
	}
	
	public Unit createUnit(int x, int y){
		Unit u = new Unit(x, y, Unit.UNIT_SIZE, Unit.UNIT_SIZE);
		return u;
	}
	
	public Element getElement(Element e, int x, int y){
		if(e instanceof Road){
			return new Road(x, y, e.getWidth(), e.getHeight());
		}else{
			return new Unit(x, y, e.getWidth(), e.getHeight());
		}
		
	}

}
